package com.musemun.service.impl;

import com.musemun.pojo.Crifo;
import com.musemun.pojo.Cropifo;
import com.musemun.mapper.CropifoMapper;
import com.musemun.pojo.Employee;
import com.musemun.pojo.EmployeeRepository;
import com.musemun.service.CropifoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author winner
 * @since 2020-04-27
 */
@Service
public class CropifoServiceImpl extends ServiceImpl<CropifoMapper, Cropifo> implements CropifoService {
    String[] status={"在馆","入库"};
    @Override
    public void insertCropifo(List<Crifo> crifoList,int i) {

        List<Cropifo> list = crifoList.stream().map(data ->
                new Cropifo(data.getCrNo(),getEmployee().getENo(),status[i],new Date()))
                .collect(Collectors.toCollection(ArrayList::new));

        this.saveBatch(list);
    }

    @Override
    public void insertCropifo(Crifo crifo,int i){
        Cropifo cropifo = new Cropifo(crifo.getCrNo(), getEmployee().getENo(), status[i], new Date());
        this.save(cropifo);
    }


    public Employee getEmployee(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        return new EmployeeRepository().findByUsername(name);
    }
}